package com.whfc.ms.api.emp.controller;

import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppEmpRewardDTO;
import com.whfc.emp.param.AppEmpRewardAddParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsRewardService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description 人员奖惩
 * @date 2020-08-12
 */
//@RequiresPermissions("emp/reward")
@RestController
@RequestMapping("/ms/api/emp/reward")
public class AppEmpRewardController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsRewardService msRewardService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("projectId") Integer projectId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "startDate", required = false) Date startDate,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "endDate", required = false) Date endDate,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("奖惩记录列表,loginer:{},projectId:{},pageNum:{},pageSize:{},startDate:{},endDate:{},keyword:{}",
                user.getUsername(), projectId, pageNum, pageSize, startDate, endDate, keyword);
        PageData<AppEmpRewardDTO> pageData = msRewardService.list(projectId, pageNum, pageSize, startDate, endDate, keyword);
        return ResultUtil.success(pageData);
    }

    @RequiresPermissions("emp/reward/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody AppEmpRewardAddParam param) {
        logger.info("添加奖惩记录,loginer:{},param:{}", user.getUsername(), param.toString());
        msRewardService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("emp/reward/delete")
    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("id") Integer id) {
        logger.info("删除奖惩记录,loginer:{},id:{}", user.getUsername(), id);
        msRewardService.del(id);
        return ResultUtil.success();
    }
    @RequiresPermissions("emp/reward/deleteList")
    @PostMapping("/delList")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                     @RequestBody List<Integer> ids) {
        logger.info("批量删除奖惩记录,loginer:{},id:{}", user.getUsername(), ids);
        msRewardService.delList(ids);
        return ResultUtil.success();
    }
}
